//
//  WLMTabBar.swift
//  WeiBo
//
//  Created by Eric on 2017-01-04.
//  Copyright © 2017 Eric. All rights reserved.
//

import UIKit

class WLMTabBar: UITabBar {
    var composeClosure : (()->())?
    
    override init(frame:CGRect){
        super.init(frame: frame)
        //添加中间按钮
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        
        super.init(coder: aDecoder)
        //添加中间按钮
        setupUI()
    }
    
    private func setupUI() {
        self.addSubview(composeBtn)
        composeBtn.addTarget(self, action: #selector(didClick), for: .touchUpInside)
    }
    
    
   @objc private func didClick() {
        
        composeClosure?()
    }
    
    
    //布局标签栏
    override func layoutSubviews() {
        //调用super
        super.layoutSubviews()
        
        //获取子控件宽高
        let width = UIScreen.main.bounds.width / 5
        let height = bounds.height
        
        //递增布局
        var index = 0
        //遍历子视图
        for subview in subviews {
            //判断类型
            if subview.isKind(of: NSClassFromString("UITabBarButton")!) {
                //重新布局
                subview.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: height)
                index += (index == 1 ? 2 : 1)
            }
        }
        composeBtn.center = CGPoint(x: self.center.x, y: height * 0.5 - 10)
        composeBtn.bounds.size = CGSize(width: width, height: height)
    }
    
    //懒加载
    lazy var composeBtn: UIButton = {
    
    
        let btn = UIButton(type: .custom)
        
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        return btn
    }()
    
    
    

}
